package com.wxdc.nmgyt.nmgytwydc.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.wxdc.nmgyt.nmgytwydc.R;
import com.wxdc.nmgyt.nmgytwydc.activity.ToCommitDataActivity;
import com.wxdc.nmgyt.nmgytwydc.model.CommitDataBean;

import java.util.List;

/**
 * Created by Administrator on 2018/5/29.
 */

public class CommitData_RecyclerViewAdapter extends RecyclerView.Adapter<CommitData_RecyclerViewAdapter.MyViewHolder> implements View.OnClickListener, View.OnLongClickListener, View.OnTouchListener {
    private final String TAG = this.getClass().getSimpleName();
    private Context mContext;
    private List<CommitDataBean> mCommitDataBeans;

    public CommitData_RecyclerViewAdapter(Context context, List<CommitDataBean> list) {
        mContext = context;
        this.mCommitDataBeans = list;
    }


    @Override
    public void onClick(View v) {
        if (mViewOnItemClickListener != null) {
            mViewOnItemClickListener.onImageViewClick(v, (Integer) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mViewOnItemClickListener != null) {
            mViewOnItemClickListener.onItemLongClick(v, (Integer) v.getTag());
        }
        return false;
    }

    private int count = 0;
    private long firstClick = 0;
    private long secondClick = 0;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        if (MotionEvent.ACTION_UP == event.getAction()) {
            count++;
            if (count == 1) {
                firstClick = System.currentTimeMillis();
            } else if (count == 2) {
                secondClick = System.currentTimeMillis();
                if (secondClick - firstClick < 500) {//实现双击
                    if (mViewOnItemClickListener != null) {
                        mViewOnItemClickListener.onDoubleClick(v, (Integer) v.getTag());
                    }
                    firstClick = 0;
                    count = 0;
                } else {
                    firstClick = secondClick;
                    count = 1;
                }
            }
        }
        return false;
    }

    //--------------------------------------------------------------
    private View view;

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.activity_tocommitdata_item, parent, false);
        view.setOnTouchListener(this);
        view.setOnLongClickListener(this);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        CommitDataBean commitDataBean = mCommitDataBeans.get(position);
        holder.itemView.setTag(position);
        holder.uploadPicImagView.setTag(position);
        if (!ToCommitDataActivity.is_in_actionMode) {
            holder.mCheckBox.setVisibility(View.GONE);
        } else {
            holder.mCheckBox.setVisibility(View.VISIBLE);
            holder.mCheckBox.setChecked(commitDataBean.isSelect);
            holder.mCheckBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    commitDataBean.isSelect = holder.mCheckBox.isChecked();
                    if (mViewOnItemClickListener != null) {
                        mViewOnItemClickListener.checkBoxClick(v, position);
                    }
                }
            });
        }
        int ydPicNums = 0,cbPicNums = 0,picNums = 0 ;
        if (commitDataBean.getYdInfoBean() != null && commitDataBean.getYdInfoBean().getIMGPATH() != null && !commitDataBean.getYdInfoBean().getIMGPATH().isEmpty()) {
            //ydPicNums = commitDataBean.getYdInfoBean().getIMGPATH().split(",").length;
            /*if ("".equals(commitDataBean.getYdInfoBean().getIMGNAME_E())) {
                ydPicNums = ydPicNums + 1;
            }
            if ("".equals(commitDataBean.getYdInfoBean().getIMGNAME_S())) {
                ydPicNums = ydPicNums + 1;
            }
            if ("".equals(commitDataBean.getYdInfoBean().getIMGNAME_N())) {
                ydPicNums = ydPicNums + 1;
            }
            if ("".equals(commitDataBean.getYdInfoBean().getIMGNAME_W())) {
                ydPicNums = ydPicNums + 1;
            }*/
        }
        if (commitDataBean.getCbYfInfoBeanList() != null) {//所有草本样方相片
            int len = commitDataBean.getCbYfInfoBeanList().size();
            for (int i = 0 ; i < len; i++) {
              /*  if (commitDataBean.getCbYfInfoBeanList().get(i).getIMGPATH() != null && ! commitDataBean.getCbYfInfoBeanList().get(i).getIMGPATH().isEmpty() && commitDataBean.getCbYfInfoBeanList().get(i).getISUPLOADPIC() == 0) {
                    //cbPicNums += commitDataBean.getCbYfInfoBeanList().get(i).getIMGPATH().split(",").length;
                   *//* if ("".equals(commitDataBean.getCbYfInfoBeanList().get(i).getIMGNAME())) {
                        cbPicNums = cbPicNums + 1;
                    }*//*
                }*/
                /*if (commitDataBean.getCbYfInfoBeanList().get(i).getWzInfos() != null) {
                    int cbWzNums = commitDataBean.getCbYfInfoBeanList().get(i).getWzInfos().size();
                    for (int j = 0; j < cbWzNums ; j++) {
                        if (commitDataBean.getCbYfInfoBeanList().get(i).getWzInfos().get(j).getYSZPICURL() != null && !commitDataBean.getCbYfInfoBeanList().get(i).getWzInfos().get(j).getYSZPICURL().isEmpty() && commitDataBean.getCbYfInfoBeanList().get(i).getWzInfos().get(j).getISUPLOADPIC() == 0) {
                            cbWzPicNums += 1;
                        }
                    }
                }*/
            }
        }
        Log.i(TAG, "onBindViewHolder: ----------------picNums:"+picNums+"-----cbPicNums:"+cbPicNums+"------ydPicNums:"+ydPicNums);
        picNums = ydPicNums + cbPicNums ;
        holder.bind(commitDataBean, picNums);
    }

    @Override
    public int getItemCount() {
        return mCommitDataBeans == null ? 0 : mCommitDataBeans.size();
    }

    //---------------------------------------------------------------------------------------------
    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView jcdInfoTxtView, cbfzyfTxtView, gmfzyfTxtView,pdfzyfTxtView, uploadPicTxtView;
        private ImageView uploadPicImagView;
        private CheckBox mCheckBox;

        public MyViewHolder(View itemView) {
            super(itemView);
            jcdInfoTxtView = itemView.findViewById(R.id.commitdata_ydinfo_txtview);
            cbfzyfTxtView = itemView.findViewById(R.id.cbfzyf_txtview);
            gmfzyfTxtView = itemView.findViewById(R.id.gmfzyf_txtview);
            pdfzyfTxtView = itemView.findViewById(R.id.pdyf_txtview);
            uploadPicTxtView = itemView.findViewById(R.id.upload_pic_txtview);
            uploadPicImagView = itemView.findViewById(R.id.uplode_pic_imgview);
            mCheckBox = itemView.findViewById(R.id.item_checkbox);
            uploadPicImagView.setOnClickListener(CommitData_RecyclerViewAdapter.this);
        }

        public void bind(CommitDataBean commitDataBean, int picNum) {
            //jcdInfoTxtView.setText(commitDataBean.getYdInfoBean().getSAMPLEPLOTNAME());
            cbfzyfTxtView.setText(commitDataBean.getCbYfInfoBeanList() == null ? String.valueOf(0) : String.valueOf(commitDataBean.getCbYfInfoBeanList().size()));
            gmfzyfTxtView.setText(commitDataBean.getGmYfInfoBeanList() == null ? String.valueOf(0): String.valueOf(commitDataBean.getGmYfInfoBeanList().size()));
            pdfzyfTxtView.setText(commitDataBean.getPdYfInfoBean() == null ? String.valueOf(0) : String.valueOf(1));
            Log.i("TAG", "bind: ----------" + picNum + "================" + String.valueOf(picNum));
            uploadPicTxtView.setText(String.valueOf(picNum));
            uploadPicTxtView.setTextColor(mContext.getResources().getColor(R.color.red));


        }
    }

    //---------------------------------------------------------------------------------------------
    private RecycleViewOnClickListener mViewOnItemClickListener;

    public interface RecycleViewOnClickListener {
        void onItemLongClick(View view, int position);

        void onImageViewClick(View view, int position);

        void onDoubleClick(View view, int position);

        void checkBoxClick(View view, int position);
    }

    //---------------------------------------------------------------------------------------------
    public void setItemClickListener(RecycleViewOnClickListener listener) {
        mViewOnItemClickListener = listener;
    }

    public void remove(CommitDataBean bean) {
        mCommitDataBeans.remove(bean);
    }

    public CommitDataBean getItem(int position) {
        return mCommitDataBeans.get(position);
    }
}
